package com.chiang.security.springmvc.config;

import com.chiang.security.springmvc.interceptor.SimpleAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import sun.java2d.pipe.SpanShapeRenderer;

/**
 * @author : 765819328@qq.com
 * @version :
 * @date : Created in 2021/8/1 15:04
 * @description: Web Config 相当与springmvc.xml文件
 * @modified By:
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.chiang.security.springmvc"
        , includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)})

public class WebConfig implements WebMvcConfigurer {

    @Autowired
    SimpleAuthenticationInterceptor simpleAuthenticationInterceptor;

    //视图解析器
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    //登录信息
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        System.out.println("test");
        registry.addViewController("/").setViewName("login");
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(simpleAuthenticationInterceptor).addPathPatterns("/r/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
